package com.tywj.buscustomerapp.model;

import android.content.Context;
import android.util.Log;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.tywj.buscustomerapp.Config;
import com.tywj.buscustomerapp.model.bean.BuyedBean;
import com.tywj.buscustomerapp.model.bean.RequestBean;
import com.tywj.buscustomerapp.model.bean.StationBean;
import com.tywj.buscustomerapp.model.bean.YuPiaoBean;
import com.tywj.buscustomerapp.presenter.contract.BuyTicketContract;
import com.tywj.buscustomerapp.utils.WebServicrUtils;
import com.tywj.buscustomerapp.view.activity.MainActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParserException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;

public class BuyTicketModel implements BuyTicketContract.BuyTicketModel {
    @Override
    public List<StationBean> loadStations(String stationId) {
        String stringIo = null;
        List<StationBean> beans = null;
        try {
            stringIo = WebServicrUtils.getInstance().getTravelLineInfo(stationId);
            Log.i("taylor",stringIo);
            InputStream in_nocode = new ByteArrayInputStream(stringIo.getBytes("UTF-8"));
            beans = WebServicrUtils.getInstance().getStations(in_nocode);
            return beans;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return beans;
    }

    @Override
    public String checkIsCanBuyTicket(String time, String lineId, String start, String end) {
        try {
            String msg = WebServicrUtils.getInstance().checkTicket(time,lineId,start,end);
            return msg;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String getCont(String time, String lineId, String start, String end) {
        try {
            String msg = WebServicrUtils.getInstance().getCount(time,lineId,start,end);
            return msg;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String getPiaoJia(String lineId, String start, String end) {
        try {
            String msg = WebServicrUtils.getInstance().getTicketPrice(lineId,start,end);
            return msg;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String doBuyTicketOnOnce(String phone, String lineNumber, String start, String end, String userStart,
                                    String userEnd, String busTime, String buyTIme, String price, String state,
                                    String city,String busNumber,String startTime,String endTime) {
        try {
            String msg = WebServicrUtils.getInstance().doBuyTicketOnce(phone,lineNumber,start,end,userStart,
                    userEnd,busTime,buyTIme,price,state,city,busNumber,startTime,endTime);
            return msg;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String doCheckMonthTicket(String year, String month, String lineNumber, String start, String end) {
        try {
            String msg = WebServicrUtils.getInstance().checkMonthTicket(year,month,lineNumber,start,end);
            Log.i("taylor",msg);
            return msg;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String doBuyMonthTicket(String phone, String lineNumber, String month, String year, String start, String end, String startTime, String endTime) {
        return null;
    }

    public String doBuyMonthTicket(String phone, String lineNumber, String month, String year, String start, String end,
                                   String busNumber,String startTime,String endTime,String state) {
        try {
            String msg = WebServicrUtils.getInstance().buyMonthTicket(phone,lineNumber,month,year,start,end,busNumber,startTime,endTime,state);
            Log.i("taylor1",msg);
            return msg;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        return null;
    }


    public RequestBean doBuyOnceTicket(final BuyTicketContract.BuyTicketView view, Context context, String yhm, String ch, String gmqsz, String gmzdz, List<String> ccsjs,
                                       String scsj, String xcsj,int number){
        //创建一个请求队列
        RequestQueue requestQueue = Volley.newRequestQueue(context);
        //创建一个请求
        String url = Config.SBURL + "/order/chepiaoinsert";
        JSONObject object = new JSONObject();
        try {
            object.put("yhm",yhm);
            object.put("ch",ch);
            object.put("gmqszId",gmqsz);
            object.put("gmzdzId",gmzdz);
//            object.put("ccsj",ccsj);
            JSONArray jsonArray = new JSONArray();
            for (int i = 0; i < ccsjs.size(); i++) {
                jsonArray.put(ccsjs.get(i));
            }
            object.put("ccsjs",jsonArray);
            object.put("scsj",scsj);
            object.put("xcsj",xcsj);
            JSONArray jsonArray1 = new JSONArray();
            for (int i = 0; i < ccsjs.size(); i++) {
                jsonArray1.put(number);
            }
            object.put("nums",jsonArray1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.i("taylor",String.valueOf(jsonObject));
                RequestBean data = new Gson().fromJson(String.valueOf(jsonObject),RequestBean.class);
                if (data!=null){
                    view.loadeIsBuySuccess(data);
                }else{
                    view.loadError("购买失败");
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                view.loadError("购买失败");
            }
        });
        //将创建的请求添加到队列中
        requestQueue.add(jsonObjectRequest);
        return null;
    }

    @Override
    public RequestBean doBuyMonthTicket(final BuyTicketContract.BuyTicketView view, Context context, String phone, String lineNumber, String month, String year, String start, String end, String startTime, String endTime) {
        //创建一个请求队列
        RequestQueue requestQueue = Volley.newRequestQueue(context);
        //创建一个请求
        String url = Config.SBURL + "/order/yuepiaoinsert";
        JSONObject object = new JSONObject();
        try {
            object.put("yhm",phone);
            object.put("gmxl",lineNumber);
            object.put("gmyf",month);
            object.put("gmnf",year);
            object.put("gmqsz",start);
            object.put("gmzdz",end);
            object.put("scsj",startTime);
            object.put("xcsj",endTime);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.i("taylor",String.valueOf(jsonObject));
                view.closeLoading();
                RequestBean data = new Gson().fromJson(String.valueOf(jsonObject),RequestBean.class);
                if (data!=null){
                    view.loadBuyMonthSuccess(data);
                }else{
                    view.loadError("购买失败");
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                view.closeLoading();
                view.loadError("购买失败");
            }
        });
        //将创建的请求添加到队列中
        requestQueue.add(jsonObjectRequest);
        return null;
    }

    @Override
    public List<YuPiaoBean> getDays(String lineNum) {
        try {
            String msg = WebServicrUtils.getInstance().getDays(lineNum);
            InputStream in_nocode = new ByteArrayInputStream(msg.getBytes("UTF-8"));
            List<YuPiaoBean> list  = WebServicrUtils.getInstance().getLineDays(in_nocode);
//            Log.i("taylor",list.toString());
            return list;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<BuyedBean> getBuyeds(String lineNum, String phone) {
        try {
            String msg = WebServicrUtils.getInstance().getBuyeds(lineNum,phone);
            InputStream in_nocode = new ByteArrayInputStream(msg.getBytes("UTF-8"));
            List<BuyedBean> list  = WebServicrUtils.getInstance().getLineBuys(in_nocode);
//            Log.i("taylor",list.toString());
            return list;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String getMyDaysTicket(String phone) {
        return null;
    }
}
